import { NextRequest, NextResponse } from 'next/server'

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const projectId = id
    
    // 模拟资源数据
    const resources = [
      {
        _id: 'resource-001',
        name: '电机控制原理图.pdf',
        type: 'application/pdf',
        category: 'schematic',
        size: 2048576, // 2MB
        url: '/files/motor-control-schematic.pdf',
        uploadedBy: 'user-001',
        uploadedByName: '张工程师',
        uploadedAt: '2024-01-10T09:00:00Z',
        updatedAt: '2024-01-10T09:00:00Z',
        projectId,
        description: '电机控制系统的完整原理图，包含所有接口和保护电路'
      },
      {
        _id: 'resource-002',
        name: 'PCB设计文件.zip',
        type: 'application/zip',
        category: 'pcb',
        size: 5242880, // 5MB
        url: '/files/pcb-design.zip',
        uploadedBy: 'user-003',
        uploadedByName: '王硬件工程师',
        uploadedAt: '2024-01-12T14:30:00Z',
        updatedAt: '2024-01-12T14:30:00Z',
        projectId,
        description: '电机驱动PCB设计文件，包含Gerber文件和BOM表'
      },
      {
        _id: 'resource-003',
        name: '产品规格书.docx',
        type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        category: 'spec',
        size: 1024000, // 1MB
        url: '/files/product-specification.docx',
        uploadedBy: 'user-002',
        uploadedByName: '李技术员',
        uploadedAt: '2024-01-15T10:00:00Z',
        updatedAt: '2024-01-15T10:00:00Z',
        projectId,
        description: '完整的产品规格说明书，包含技术参数和性能指标'
      },
      {
        _id: 'resource-004',
        name: '测试报告.pdf',
        type: 'application/pdf',
        category: 'test_report',
        size: 1536000, // 1.5MB
        url: '/files/test-report.pdf',
        uploadedBy: 'user-004',
        uploadedByName: '赵测试工程师',
        uploadedAt: '2024-01-18T16:00:00Z',
        updatedAt: '2024-01-18T16:00:00Z',
        projectId,
        description: '电机控制器的完整测试报告，包含所有测试项目和结果'
      },
      {
        _id: 'resource-005',
        name: '源代码.tar.gz',
        type: 'application/gzip',
        category: 'code',
        size: 10485760, // 10MB
        url: '/files/source-code.tar.gz',
        uploadedBy: 'user-001',
        uploadedByName: '张工程师',
        uploadedAt: '2024-01-20T11:00:00Z',
        updatedAt: '2024-01-20T11:00:00Z',
        projectId,
        description: '电机控制器的完整源代码，包含所有模块和驱动程序'
      },
      {
        _id: 'resource-006',
        name: '结构设计图.dwg',
        type: 'application/acad',
        category: 'structure',
        size: 3072000, // 3MB
        url: '/files/structure-design.dwg',
        uploadedBy: 'user-005',
        uploadedByName: '钱结构工程师',
        uploadedAt: '2024-01-22T13:30:00Z',
        updatedAt: '2024-01-22T13:30:00Z',
        projectId,
        description: '电机控制器外壳结构设计图，包含尺寸和材料要求'
      },
      {
        _id: 'resource-007',
        name: '认证报告.pdf',
        type: 'application/pdf',
        category: 'report',
        size: 2560000, // 2.5MB
        url: '/files/certification-report.pdf',
        uploadedBy: 'user-006',
        uploadedByName: '孙认证工程师',
        uploadedAt: '2024-01-25T15:00:00Z',
        updatedAt: '2024-01-25T15:00:00Z',
        projectId,
        description: '产品认证报告，包含CE和FCC认证结果'
      }
    ]

    return NextResponse.json(resources)
  } catch (error) {
    console.error('获取项目资源失败:', error)
    return NextResponse.json(
      { error: '获取项目资源失败' },
      { status: 500 }
    )
  }
}

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const projectId = id
    const body = await request.json()
    
    // 这里应该上传新资源的逻辑
    console.log('上传新资源:', { projectId, ...body })
    
    return NextResponse.json({ 
      message: '资源上传成功',
      resource: {
        _id: `resource-${Date.now()}`,
        name: body.name,
        type: body.type,
        category: body.category,
        size: body.size,
        url: body.url,
        uploadedBy: 'current-user',
        uploadedByName: '当前用户',
        uploadedAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        projectId,
        description: body.description
      }
    })
  } catch (error) {
    console.error('上传项目资源失败:', error)
    return NextResponse.json(
      { error: '上传项目资源失败' },
      { status: 500 }
    )
  }
}